package com.example.asset.utils;

import jakarta.servlet.http.HttpServletRequest;

/**
 * 创建时间:2025/2/18 17:33
 * 创建人:pmc
 * 描述:
 */
public class UtilIP{
    public static final String[] HEADERS_TO_TRY = {"X-Forwarded-For", "Proxy-Client-IP", "WL-Proxy-Client-IP", "HTTP_X_FORWARDED_FOR", "HTTP_X_FORWARDED", "HTTP_X_CLUSTER_CLIENT_IP", "HTTP_CLIENT_IP", "HTTP_FORWARDED_FOR", "HTTP_FORWARDED", "HTTP_VIA", "REMOTE_ADDR", "X-Real-IP"};

    /***
     * 获取客户端ip地址(可以穿透代理)
     * @param request
     * @return
     */
    public static String getIpAddress(HttpServletRequest request){
        for(String header : HEADERS_TO_TRY){
            String ip = request.getHeader(header);
            if(ip != null && ip.length() != 0 && !"unknown".equalsIgnoreCase(ip)){
                return ip;
            }
        }
        return request.getRemoteAddr();
    }
}